﻿using B.Auth.APi.Application.Command.Menus;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Menuss
{
    public class UpdataMenusCommandHand : IRequestHandler<UpdataMenusCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<MenuModel> _menu;

        public UpdataMenusCommandHand(IBaseRepository<MenuModel> menu)
        {
            _menu = menu;
        }

        public Task<ApiResult<int>> Handle(UpdataMenusCommand request, CancellationToken cancellationToken)
        {

            var result=new ApiResult<int>();


            var menu=new MenuModel();
            menu.Id = request.Id;
            menu.MenuName = request.MenuName;
            menu.Url = request.Url;
            menu.PID = request.PID;
            menu.CreateTime = DateTime.Now;
            menu.UpdateTime =DateTime.Now;
            menu.CreateUserName = "张三";
            menu.UodateUserName = "张三";


            var stae = _menu.GetAll().Any(x => x.Id != request.Id && x.PID == request.PID && x.MenuName == request.MenuName && x.MenuDel == 0);
            if (stae)
            { 
                result.Code=ApiEnum.fail;
                result.Message = "同一菜单下不允许名称重复";
                return Task.FromResult(result);


            }
            
            _menu.Update(menu);

            result.Message = "修改成功";
            result.Code = ApiEnum.ok;


            return Task.FromResult(result);





        }
    }
}
